function sc = spherecoords(c)
% spherecoords  - compute spherical coordinates
%
% FORMAT:       sc = spherecoords(c)
%
% Input fields:
%
%       c           Cx3 coordinates
%
% Output fields:
%
%       sc          Cx3 spherical coordinations where
%                   sc(:, 1) := radius
%                   sc(:, 2) := acos(c(:, 3) / radius)
%                   sc(:, 3) := atan2(c(:, 2), c(:, 1))

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin ~= 1 || ...
   ~isa(c, 'double') || ...
    ndims(c) > 2 || ...
    size(c, 2) ~= 3
    error( ...
        'BVQXtools:BadArgument', ...
        'Bad or missing argument.' ...
    );
end

% computation
sc = zeros(size(c));
sc(:, 1) = sqrt(sum(c .* c, 2));
sc(:, 2) = acos(c(:, 3) ./ sc(:, 1));
sc(:, 3) = atan2(c(:, 2), c(:, 1));
